package edu.nf.project.webs.storeOperation;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.entity.storeOperation.convention.inspect.SelfInspection;
import edu.nf.project.services.exception.derivedAnomalies.SelfInspectionException;
import edu.nf.project.services.storeOperation.SelfInspectionService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自检控制器
 */
@RestController
@RequestMapping("/api/self-inspections")
public class SelfInspectionController extends BaseController {
    @Autowired
    private SelfInspectionService selfInspectionService;

    /**
     * 获取自检列表
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 自检列表
     */
    @GetMapping
    public ResultVO<Page<SelfInspection>> getSelfInspectionList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<SelfInspection> page = new Page<>(pageNum, pageSize);
            Page<SelfInspection> result = selfInspectionService.getSelfInspectionList(page);
            return success(result);
        } catch (Exception e) {
            if (e instanceof SelfInspectionException) {
                throw e;
            }
            throw new SelfInspectionException(60000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取自检详细信息
     * @param siId 自检ID
     * @return 自检信息
     */
    @GetMapping("/{siId}")
    public ResultVO<SelfInspection> getSelfInspectionInfo(@PathVariable Integer siId) {
        try {
            SelfInspection selfInspection = selfInspectionService.getSelfInspectionInfo(siId);
            return success(selfInspection);
        } catch (Exception e) {
            if (e instanceof SelfInspectionException) {
                throw e;
            }
            throw new SelfInspectionException(60000, "系统错误，请稍后重试");
        }
    }

    /**
     * 创建自检记录
     * @param selfInspection 自检信息
     * @return 是否创建成功
     */
    @PostMapping
    public ResultVO<Boolean> createSelfInspection(@RequestBody SelfInspection selfInspection) {
        try {
            boolean result = selfInspectionService.createSelfInspection(selfInspection);
            return success(result);
        } catch (Exception e) {
            if (e instanceof SelfInspectionException) {
                throw e;
            }
            throw new SelfInspectionException(60000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/list/{storeId}")
    public ResultVO<List<SelfInspection>> listSelfInspectionByStoreId(@RequestParam("year") Integer year,
                                                                      @RequestParam("month") Integer month,
                                                                      @PathVariable Integer storeId) {
        try {
            List<SelfInspection> selfInspections = selfInspectionService.listSelfInspectionByStoreId(year, month, storeId);
            return success(selfInspections);
        } catch (Exception e) {
            if (e instanceof SelfInspectionException) {
                throw e;
            }
            throw new SelfInspectionException(60000, "系统错误，请稍后重试");
        }
    }
} 